throw error from createUserContext in case of invalid input#1063
Merged
throw error from createUserContext in case of invalid input#1063
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR updates the createUserContext function to throw errors for invalid inputs instead of returning null.
- Update the return type in shared_types.ts to indicate a non-null OptimizelyUserContext
- Modify the error handling in Optimizely.createUserContext to throw errors with specific messages
- Update tests in index.tests.js to verify that errors are thrown for invalid inputs
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lib/shared_types.ts | Changed createUserContext return type from nullable to non-null to match new behavior |
| lib/optimizely/index.ts | Updated createUserContext to throw errors (INVALID_IDENTIFIER or INVALID_ATTRIBUTES) |
| lib/optimizely/index.tests.js | Modified tests to assert that errors are thrown instead of returning null |
Comment on lines
1366
to
1373
| if (userIdentifier === undefined || !this.validateInputs({ user_id: userIdentifier })) { | ||
| throw new Error(INVALID_IDENTIFIER); | ||
| } | ||
|
|
||
| if (!this.validateInputs({ user_id: userIdentifier }, attributes)) { | ||
| throw new Error(INVALID_ATTRIBUTES); | ||
| } | ||
|
|
There was a problem hiding this comment.
[nitpick] Consider reviewing the separation of input validation into two distinct checks; if combining them is feasible without losing error specificity, it might simplify the flow.
Suggested change
| if (userIdentifier === undefined || !this.validateInputs({ user_id: userIdentifier })) { | |
| throw new Error(INVALID_IDENTIFIER); | |
| } | |
| if (!this.validateInputs({ user_id: userIdentifier }, attributes)) { | |
| throw new Error(INVALID_ATTRIBUTES); | |
| } | |
| if ( | |
| userIdentifier === undefined || | |
| !this.validateInputs({ user_id: userIdentifier }, attributes) | |
| ) { | |
| throw new Error( | |
| userIdentifier === undefined || !this.validateInputs({ user_id: userIdentifier }) | |
| ? INVALID_IDENTIFIER | |
| : INVALID_ATTRIBUTES | |
| ); | |
| } |
junaed-optimizely
approved these changes
May 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Test plan
-updated tests
Issues